package cn.cyh.org.service.impl;

import cn.cyh.basic.exception.BusinessException;
import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.org.domain.Employee;
import cn.cyh.org.domain.Shop;
import cn.cyh.org.mapper.EmployeeMapper;
import cn.cyh.org.mapper.ShopMapper;
import cn.cyh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


// 支持事务 (如果有事务就加入事务，没有就不加入)
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    //  自动注入
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void settlement(Shop shop) {
        // 校验 Username phone email
        // 判断两次密码是否一致 不一致就抛出业务异常 （自定义业务异常）
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }
        // 检查用户是否被注册
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if (employee!=null){
            throw new BusinessException("账户已经存在");
        }
        // 入驻
        // 保存员工信息返回自增id
        employeeMapper.save(admin);
        // 保存店铺信息
        shop.setAdmin(admin);
        shopMapper.save(shop);
        // 修改员工所属店铺id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }
}
